/*
 * Created on 01-Sep-2006
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package wstrader.task.quality;

import java.sql.ResultSet;
import java.util.Vector;

import pc.sql.value.BooleanValue;
import pc.sql.value.DateValue;
import pc.sql.value.IntValue;

import pc.system.Record;
import pc.system.Tasks;

/**
 * @author NANSA
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class SampleSent extends SampleTask
{
	  public SampleSent(Tasks owner)
	  {
	    super(owner,"samplesent");
	  }
	  
	  public long doTask()
	  {
	    int task_id     = this.getRequest().getIntValue("task_id");
	    long focused_id = this.getRequest().getLongValue("sample_master");
	    int RecId = this.getRequest().getIntValue("rec_id");
	    
	    switch (task_id)
		{
	      case 1 : // Update
	      	if (RecId > 0)
	      	{
	      		focused_id = doUpdate("sample_master",focused_id);
	      	}
	        break;
	      case 2 : // Delete

	        break;
	      default : //View only
	        break;
	    }

	    return focused_id;
	  }	  
	  
	  public ResultSet fetchSampleSentList()
	  {
	  	java.sql.Date SentDate = getRequest().getValueDate("sent_date");
	  	int SentTo = getRequest().getIntValue("sent_to");
	  	int CategoryId = getRequest().getIntValue("category_id");
	  	int OriginId = getRequest().getIntValue("origin");
	 	int GradeId = getRequest().getIntValue("grade");
	 	
	 	String Sql = "SELECT sample_master.id,sample_master.ref_number, " +
		"sample_master.sample_type,sample_master.sent_received_date, " +
		"sample_master.buyer_seller_id,sample_master.attention_to, " +
		"sample_master.forwarder_name,sample_master.awb_no, " +
		"sample_details.sample_category_id,sample_details.contract_id, " +
		"sample_details.location,sample_details.wn_id, " +
		"sample_details.origin_id,sample_details.quality_id,sample_details.grade_id, " +
		"sample_details.contract_ton,sample_details.sample_kgs,sample_details.approved_reject, " +
		"sample_details.contract_ref_no,sample_details.buyer_ref_no,sample_details.wn_ref_no, " +
		"sample_details.controller,sample_details.approved_reject_date,sample_details.confirm, " +
		"sample_details.id as DtsId " +
		"FROM sample_master " +
		"LEFT OUTER JOIN sample_details ON (sample_master.id=sample_details.sample_master_id) " +
		"where (sample_master.sent_received_date = ? OR ?) " +
		"and (sample_master.buyer_seller_id = ? OR ?) " +
		"and (sample_details.sample_category_id = ? OR ?) " +
		"and (sample_details.origin_id = ? OR ?) " +
		"and (sample_details.grade_id = ? OR ?) " +
		"and (sample_master.sample_type = 'S')";
	 	
  		Vector vp = new Vector();

  		vp.addElement(new DateValue(SentDate));
  		vp.addElement(new BooleanValue(SentDate.compareTo(new java.sql.Date(0)) == 0));

  		vp.addElement(new IntValue(SentTo));
  		vp.addElement(new BooleanValue(SentTo == 0));

  		vp.addElement(new IntValue(CategoryId));
  		vp.addElement(new BooleanValue(CategoryId == 0));

  		vp.addElement(new IntValue(OriginId));
  		vp.addElement(new BooleanValue(OriginId == 0));

  		vp.addElement(new IntValue(GradeId));
  		vp.addElement(new BooleanValue(GradeId == 0));	  		
  		
  		//vp.addElement(new StringValue("S"));
  		
  		return getSQLCommand().executeQuery(Sql,vp);	  	
	 	
	  } 
	  
	  public ResultSet fetchSampleCategoryList(int CategoryId)
	  {
	  	String Sql = "";
	  	int SampleId = getRequest().getIntValue("sample_master");
	  	Sql = "Select * from sample_details where sample_master_id = " + SampleId + " and sample_category_id = " + CategoryId;
	  	return getSQLCommand().executeQuery(Sql);
	  }
	  
      public String getNew_SamSentRef()
      {
        String sql = "SELECT MAX(ref_number) as ref_number FROM sample_master";
        Record rc = new Record(this.getSQLCommand().executeQuery(sql));
        String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : "QSS 00-00000";
        rc.close();
        return "QSS" + " " + this.getNextRefNumber(ref);
      } 
      
  
      public String getNew_SamReceivedRef()
      {
        String sql = "SELECT MAX(ref_number) as ref_number FROM sample_master";
        Record rc = new Record(this.getSQLCommand().executeQuery(sql));
        String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : "QSS 00-00000";
        rc.close();
        return "QRS" + " " + this.getNextRefNumber(ref);
      }      
      
}
